package com.example.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.tree.Tree;
import com.example.commons.core.domain.BaseResponse;
import com.example.commons.core.entity.RoleMenuRelation;
import com.example.commons.core.entity.SystemMenu;
import com.example.system.api.service.UserInfoDubboService;
import com.example.system.service.RoleMenuRelationService;
import com.example.system.service.SystemMenuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("sys/menu")
public class SystemMenuController {

    private final SystemMenuService systemMenuService;
    private final RoleMenuRelationService roleMenuRelationService;
    @DubboReference
    private UserInfoDubboService userInfoDubboService;

    public SystemMenuController(SystemMenuService systemMenuService, RoleMenuRelationService roleMenuRelationService) {
        this.systemMenuService = systemMenuService;
        this.roleMenuRelationService = roleMenuRelationService;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @SaCheckLogin
    @GetMapping("getRouters")
    public BaseResponse<List<Tree<String>>> getRouters() {
        var roles = userInfoDubboService.userRoleRelationFindByUserId(Long.parseLong(StpUtil.getLoginId().toString()));
        if (roles == null || roles.size() == 0) {
            return BaseResponse.ok();
        }
        return BaseResponse.ok(systemMenuService.getRouteMenuList(roles.get(0)));
    }

    /**
     * 新增或修改系统菜单
     *
     * @param systemMenu 系统菜单
     * @return 新增成功
     */
    @SaCheckLogin
    @PostMapping("addOrUpdateMenu")
    public BaseResponse<Void> addOrUpdateMenu(@RequestBody SystemMenu systemMenu) {
        systemMenuService.addSystemMenu(systemMenu);
        if(systemMenu.getMenuId() == null){
            roleMenuRelationService.save(RoleMenuRelation
                    .builder()
                    .menuId(systemMenu.getMenuId())
                    .roleId(Long.parseLong(StpUtil.getLoginId().toString()))
                    .createTime(LocalDateTime.now())
                    .build());
        }
        return BaseResponse.ok();
    }

    /**
     * 删除系统菜单
     *
     * @param menuId 系统菜单id
     * @return 删除成功
     */
    @SaCheckLogin
    @DeleteMapping("delMenu/{menuId}")
    public BaseResponse<Void> delMenu(@PathVariable("menuId") Long menuId) {
        systemMenuService.removeById(menuId);
        return BaseResponse.ok();
    }
}
